<template>
  <el-dialog v-model="visible" :title="!dataForm.purchaseAnnouncementId ? '新增' : '修改'" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item  prop="purchaseProjectId" label="采购项目">
        <el-select :disabled="!dataForm.purchaseAnnouncementId ? false : true" v-model="dataForm.purchaseProjectId" single placeholder="请选则采购项目">
          <el-option v-for="role in purchaseProjectList" :key="role.purchaseProjectId" :label="role.purchaseProjectName" :value="role.purchaseProjectId"></el-option>
        </el-select>
      </el-form-item>
          <el-form-item label="公告名称" prop="announcementName">
        <el-input v-model="dataForm.announcementName" placeholder="公告名称"></el-input>
      </el-form-item>
<!--          <el-form-item label="公告性质" prop="announcementNature">-->
<!--            <ren-radio-group v-model="dataForm.announcementNature" dict-type="announcement_nature"></ren-radio-group>-->
<!--      </el-form-item>-->
          <el-form-item label="报名开始时间" prop="applyStartTime">
            <el-date-picker v-model="dataForm.applyStartTime" type="applyStartTime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" placeholder="奖补申请开始时间"></el-date-picker>
<!--            <el-input v-model="dataForm.applyStartTime" placeholder="报名开始时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
      </el-form-item>
          <el-form-item label="报名结束时间" prop="applyEndTime">
            <el-date-picker v-model="dataForm.applyEndTime" type="applyEndTime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" placeholder="奖补申请开始时间"></el-date-picker>
<!--            <el-input v-model="dataForm.applyEndTime" placeholder="报名结束时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
      </el-form-item>
<!--          <el-form-item label="公告状态" prop="isPublish">-->
<!--            <ren-radio-group v-model="dataForm.isPublish" dict-type="announcement_status"></ren-radio-group>-->
<!--      </el-form-item>-->
          <el-form-item label="公告发布时间" prop="publishTime">
            <el-date-picker v-model="dataForm.publishTime" type="publishTime" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss" placeholder="奖补申请开始时间"></el-date-picker>
<!--            <el-input v-model="dataForm.publishTime" placeholder="公告发布时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
      </el-form-item>
      <el-form-item label="公告详情">
        <el-button type="primary" @click="uploadHandle()">上传文件</el-button>
      </el-form-item>
      <el-form-item label="" prop="">
        <div v-for="(value, key, index) in fileList" :key="key" style="display: flex; flex-wrap: wrap; gap: 10px;">
          <div> {{ value.fileName }} </div>
          <div>
            <el-button type="primary" link @click="deleteFile(key)">删除</el-button><br>
          </div>
        </div>
      </el-form-item>
<!--          <el-form-item label="项目创建人" prop="createUser">-->
<!--        <el-input v-model="dataForm.createUser" placeholder="项目创建人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="项目创建时间，格式：YYYY-MM-DD HH:MM:SS" prop="createTime">-->
<!--        <el-input v-model="dataForm.createTime" placeholder="项目创建时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="最后更新人" prop="updateUser">-->
<!--        <el-input v-model="dataForm.updateUser" placeholder="最后更新人"></el-input>-->
<!--      </el-form-item>-->
<!--          <el-form-item label="最后更新时间，格式：YYYY-MM-DD HH:MM:SS" prop="updateTime">-->
<!--        <el-input v-model="dataForm.updateTime" placeholder="最后更新时间，格式：YYYY-MM-DD HH:MM:SS"></el-input>-->
<!--      </el-form-item>-->
      </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
      <!-- 弹窗, 上传文件 -->
      <upload v-if="uploadVisible" ref="uploadRef" @fileUpload="handleMessage" ></upload>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { nextTick, reactive, ref } from "vue";
import baseService from "@/service/baseService";
import { ElMessage } from "element-plus";
const emit = defineEmits(["refreshDataList"]);
import { isEmail, isMobile } from "@/utils/utils";
import { IObject } from "@/types/interface";
import Upload from "../oss/file-upload.vue";

const uploadVisible = ref(false);
const uploadRef = ref();
const fileList = ref<any[]>([]);

const visible = ref(false);
const dataFormRef = ref();
const purchaseProjectList = ref<any[]>([]);
const dataForm = reactive({
  purchaseAnnouncementId: '',  purchaseProjectId: '',  announcementName: '',  announcementNature: '',  applyStartTime: '',  applyEndTime: '',  isPublish: '',  publishTime: '',  createUser: '',  createTime: '',  updateUser: '',  updateTime: ''});

const rules = ref({
          purchaseProjectId: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          announcementName: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          announcementNature: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          applyStartTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          applyEndTime: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ],
          fileList: [
      { required: true, message: '必填项不能为空', trigger: 'blur' }
    ]
  });

const init = (purchaseAnnouncementId?: number) => {
  visible.value = true;
    console.log("-=======================");
  fileList.value = [];
  dataForm.purchaseAnnouncementId = "";
  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  Promise.all([getPurchaseProjectList()]).then(() => {
    if (purchaseAnnouncementId) {
      getInfo(purchaseAnnouncementId);
    }
  });
};

// 获取信息
const getInfo = (purchaseAnnouncementId: number) => {
  baseService.get("/purchaseannouncement/purchaseannouncement/" + purchaseAnnouncementId).then((res) => {
    Object.assign(dataForm, res.data);
    fileList.value = res.data.fileList;
  });
};

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    dataForm.fileList = fileList.value;
    (!dataForm.purchaseAnnouncementId ? baseService.post : baseService.put)("/purchaseannouncement/purchaseannouncement", dataForm).then((res) => {
      ElMessage.success({
        message: '成功',
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

// 获取项目列表
const getPurchaseProjectList = () => {
  return baseService.get("/purchaseproject/purchaseproject/list").then((res) => {
    purchaseProjectList.value = res.data;
  });
};

defineExpose({
  init
});

// 上传文件
const uploadHandle = () => {
  uploadVisible.value = true;
  nextTick(() => {
    uploadRef.value.init();
  });
};

const handleMessage =(data) => {
  console.log("父组件接收到数据" + JSON.stringify(data._rawValue[0].response.data));
  for(let i = 0; i < data._rawValue.length; i++) {
    fileList.value.push(data._rawValue[i].response.data);
  }

}

const deleteFile =(index) => {
  console.log("-=======" + index);
  fileList.value.splice(index, 1);
}

</script>
